package com.thread.thread.CyclicBarrier;

import java.util.Arrays;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.stream.IntStream;

//回环栅栏
public class CyclicBarrierTest {

	public static void main(String[] args) {
		CyclicBarrier cyclicBarrier=new CyclicBarrier(5);

		Thread[] threads=new Thread[10];

		IntStream.range(0,threads.length).forEach((i)->{
			threads[i]=new Thread(()->{
				if(i%2==0){
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				System.out.println("线程"+i+"开始等待");

				try {
					cyclicBarrier.await();

					System.out.println("线程"+i+"开始执行");

				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (BrokenBarrierException e) {
					e.printStackTrace();
				}
			});

		});


		Arrays.asList(threads).forEach(thread->thread.start());
	}

}
